package com.odais.AE_Handler;

import java.util.Set;

import org.andengine.entity.Entity;
import org.andengine.entity.scene.Scene;

import com.odais.AE_Planner.Position;
import com.odais.AE_Planner.RoomPlanner;
import com.odais.Reservation.ReservationEntity;

public class TouchEntityFinder
{
// ===========================================================
// Constants
// ===========================================================
	private static final int SEARCH_RADIUS = 40;

// ===========================================================
// Fields
// ===========================================================
	private int IntraListCnt;
	private String currentList;
	private Entity currentEntity, currentEntityWide;
	private float distanceToEntity;
	private boolean foundEntity;

// ===========================================================
// Constructors
// ===========================================================
	public TouchEntityFinder()
	{
	}
	
// ===========================================================
// Getter & Setter
// ===========================================================


// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
	public Entity Find(Position position)
	{
		for(int i=0; i<RoomPlanner.enginePopulation.getListNames().size(); i++)
		{
			Set<String> tmpListSet = RoomPlanner.enginePopulation.getListNames();
			currentList = (String) tmpListSet.toArray()[i];
			currentEntityWide = FindInList(position, RoomPlanner.enginePopulation, currentList);
			if(currentEntityWide != null)
				break;
		}
		
		return currentEntityWide;
	}
	
	public Entity FindInList(Position position, EnginePopulation enginePopulation, String listName)
	{
		foundEntity = false;

		currentList = listName;
		if(currentList.equals(EnginePopulation.ENGINE_POPULATION_RESERVATIONENTITY))
		{
			for(IntraListCnt=0; IntraListCnt<enginePopulation.getList(currentList).size(); IntraListCnt++)
			{
				currentEntity = ((ReservationEntity) enginePopulation.getList(currentList).get(IntraListCnt)).getReservationBox_LeftPanel();
				distanceToEntity = (float) Math.sqrt(Math.pow((currentEntity.getX() - position.getPosX()),2) + Math.pow((currentEntity.getY() - position.getPosY()),2));
				
				if((distanceToEntity < SEARCH_RADIUS) && currentEntity.isVisible())
				{
					foundEntity = true;
					currentEntity = (ReservationEntity) enginePopulation.getList(currentList).get(IntraListCnt);
					currentEntity.setUserData(currentList);
					break;
				}
				
				currentEntity = ((ReservationEntity) enginePopulation.getList(currentList).get(IntraListCnt)).getReservationBox_RightPanel();
				distanceToEntity = (float) Math.sqrt(Math.pow((currentEntity.getX() - position.getPosX()),2) + Math.pow((currentEntity.getY() - position.getPosY()),2));
				
				if((distanceToEntity < SEARCH_RADIUS) && currentEntity.isVisible())
				{
					foundEntity = true;
					currentEntity = (ReservationEntity) enginePopulation.getList(currentList).get(IntraListCnt);
					currentEntity.setUserData(currentList);
					break;
				}
			}
		}
		else
		{
			for(IntraListCnt=0; IntraListCnt<enginePopulation.getList(currentList).size(); IntraListCnt++)
			{
				currentEntity = (Entity) enginePopulation.getList(currentList).get(IntraListCnt);
				distanceToEntity = (float) Math.sqrt(Math.pow((currentEntity.getX() - position.getPosX()),2) + Math.pow((currentEntity.getY() - position.getPosY()),2));
				
				if((distanceToEntity < SEARCH_RADIUS) && currentEntity.isVisible())
				{
					foundEntity = true;
					currentEntity.setUserData(currentList);
					break;
				}
				
			}
		}
		
		
		if(foundEntity)
			return currentEntity;
		else
			return null;
	}
	
}